package BVRClient;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 * The Blue vs Red: Arena Map Class.
 * @author Jesse Gill
 * @author Shannon Setter
 */
public class BVRC_Map
{
	private Image background = null;
	private Image collisions = null;
	
	private String name = "test";
	
	private BVRC_Point blueStartPosition = new BVRC_Point(185, 335);
	private BVRC_Point redStartPosition = new BVRC_Point(1110, 670);
	
	/**
	 * 
	 */
	public BVRC_Map() {}
	
	/**
	 * 
	 * @param filename
	 */
	public BVRC_Map(String filename)
	{
		this.openFromFile(filename);
	}
	
	/**
	 * 
	 * @param filename
	 * @return true if the file was opened successfully, false in all other cases
	 */
	public boolean open(String filename)
	{
		return this.openFromFile(filename);
	}
	
	/**
	 * Draw the visible part of the map file.
	 * @param visible
	 * @param x
	 * @param y
	 */
	public void draw(int x, int y)
	{
		if (background != null)
		{
			background.draw(x, y);
		}
	}
	
	/**
	 * Unzips a map file and loads all the data into the map object.
	 * @param filename
	 */
	public boolean openFromFile(String filename)
	{
		try
		{
			// error if the map is not unzipped properly
			if (! BVRC_MapUnzip.unzipMapToDirectory(filename, "res/temp/map/"))
				return false;
			
			background = new Image("res/temp/map/map.png");
			collisions = new Image("res/temp/map/collision.png");
		}
		catch (SlickException e)
		{
			e.printStackTrace();
			
			return false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			
			return false;
		}
		
		return true;
	}
	
	/**
	 * 
	 * @param r
	 * @return true if the rect collides with the map, false in all other cases
	 */
	public boolean rectMapCollision(BVRC_Rect r)
	{
		for (float x = r.x; x < (r.x + r.width); x++)
		{
			for (float y = r.y; y < (r.y + r.height); y++)
			{
				// if the pixel has a red value of 0 then it's a collision pixel (not white)
				if (collisions.getColor((int)x, (int)y).r == 0)
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	/**
	 * 
	 * @param collisions
	 */
	public void setCollisions(Image collisions)
	{
		this.collisions = collisions;
	}
	
	/**
	 * 
	 * @return the collision image map
	 */
	public Image getCollisions()
	{
		return this.collisions;
	}
	
	/**
	 * 
	 * @param name
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	
	/**
	 * 
	 * @return the map's name
	 */
	public String getName()
	{
		return this.name;
	}
	
	/**
	 * 
	 * @param blueStartPosition
	 */
	public void setBlueStartPosition(BVRC_Point blueStartPosition)
	{
		this.blueStartPosition = blueStartPosition;
	}
	
	/**
	 * 
	 * @return the blue team's starting position on the map
	 */
	public BVRC_Point getBlueStartPosition()
	{
		return this.blueStartPosition;
	}
	
	/**
	 * 
	 * @param redStartPosition
	 */
	public void setRedStartPosition(BVRC_Point redStartPosition)
	{
		this.redStartPosition = redStartPosition;
	}
	
	/**
	 * 
	 * @return the red team's starting position on the map
	 */
	public BVRC_Point getRedStartPosition()
	{
		return this.redStartPosition;
	}
}
